/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

// TODO z řádků vyčíst hodnoty

package isas;

import java.net.*;
import javax.net.ssl.HttpsURLConnection;
import javax.swing.JOptionPane;
import org.jsoup.nodes.Document;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author Kubik
 */
public class GetGrades {
    URL url = null;
    String[] predmety;
    int[] znamky;
    int[] vahy;
    
    public GetGrades() throws Exception{
        url = new URL("https://www.sspbrno.cz/ISAS/prubezna-klasifikace.php");
        HttpsURLConnection httpCon = Login.initializeConnection("", url, null);
        if(httpCon != null){
            //debug
            java.io.BufferedReader BRresponse = new java.io.BufferedReader(new java.io.InputStreamReader(httpCon.getInputStream()));
            String StrResponse = "";
            String s;
            while((s = BRresponse.readLine()) != null){
                StrResponse+= s;
            }
            Document doc = Jsoup.parse(StrResponse);
            Element table = doc.select("table[class=isas-tabulka]").first();
            Elements lines = table.getElementsByTag("tr");
            znamky = new int[lines.size() - 1];
            predmety = new String[lines.size() - 1];
            vahy = new int[lines.size() - 1];
            lines.remove(0);
            Element tmp;
            StrResponse = "";
            int j;
            for(int i = 0; i < lines.size(); i++){
                tmp = lines.get(i);
                j = 0;
                Elements tmp2;
                while((s = tmp.child(j).text()) != null) {
                    switch(j){
                        case 1: predmety[i] = s; break;
                        case 2: znamky[i] = this.getGrade(s); break;
                        case 3: vahy[i] = this.getValue(s); break;
                        default: break;
                    }
                    tmp2 = tmp.children();
                    int size = tmp2.size()-1;                    
                    //StrResponse += s;
                    if(j >= size) {
                        break;
                    }
                    j++;
                }
            }
            boolean pause = true;
            AverageCalc averageCalc = new AverageCalc(predmety, znamky, vahy);
            //debug end
        }
    }
    
    private int getValue(String s){
        return Integer.parseInt(s.replaceAll("[\\D]", ""));
    }
    
    private int getGrade(String s){
        if(s.equals("N")){
            return 5;
        }else{
            return Integer.parseInt(s);
        }
    }
}
